package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.*;
import com.example.fitnessservice.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/21 11:08
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    /**
     * 根据用户的id查询用户报的课程
     * 并且根据状态查询
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/{status}/{currentPage}/{pageSize}")
    public Result findUserCourse(@PathVariable Integer userId, @PathVariable Integer status, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<CourseDetails> userCourse = userService.findUserCourse(userId, status, currentPage, pageSize);
        System.out.println(userCourse);

        Result result = new Result();
        if (userCourse != null) {
            PageInfo<CourseDetails> pageInfo = new PageInfo<>(userCourse);
            result.setCode(200);
            result.setMsg("用户课程查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("用户课程查询失败");
        }
        return result;
    }

    /**
     * 根据输入的经纬度，对场馆进行排序，经纬度是以list方式存储的，经度输在第一个，纬度输在第二个
     * 找到所有场馆之后，并显示场馆中所有课程
     *
     * @return
     */
    @GetMapping("/distance/{x}/{y}/{currentPage}/{pageSize}")
    public Result orderDistance(@PathVariable Double x,
                                @PathVariable Double y,
                                @PathVariable Integer currentPage,
                                @PathVariable Integer pageSize
    ) {
        //距离又近到远排列，单位是km
        PageHelper.startPage(currentPage, pageSize);
        List<Place> placeList = userService.orderDistance(x, y);
        PageInfo<Place> pageInfo = new PageInfo<>(placeList);
        Result result = new Result();
        if (placeList.size() > 0) {
            result.setCode(200);
            result.setMsg("距离查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("距离查询失败");
        }
        return result;
    }

    /**
     * 根据用户id查询，用户的训练营
     * 并且根据状态查询
     * @param userId
     * @return
     */
    @GetMapping("/camp/{userId}/{status}/{currentPage}/{pageSize}")
    public Result findUserCamp(@PathVariable Integer userId, @PathVariable Integer status, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<CourseDetailsCamp> camps = userService.findUserCamp(userId, status,currentPage, pageSize);

        Result result = new Result();
        if (camps != null) {
            PageInfo<CourseDetailsCamp> pageInfo = new PageInfo<>(camps);
            result.setCode(200);
            result.setMsg("训练营查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("训练营查询失败");
        }
        return result;
    }

    /**
     * 根据用户的id查询用户报的课程
     * 所有状态的课程都查询出来
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/{currentPage}/{pageSize}")
    public Result findAllCourse(@PathVariable Integer userId, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<CourseDetails> userCourse = userService.findAllCourse(userId, currentPage, pageSize);
        System.out.println(userCourse);

        Result result = new Result();
        if (userCourse != null) {
            PageInfo<CourseDetails> pageInfo = new PageInfo<>(userCourse);
            result.setCode(200);
            result.setMsg("用户课程查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("用户课程查询失败");
        }
        return result;
    }

    /**
     * 根据用户id查询，用户的训练营
     * 并所有状态的课程都查询出来
     * @param userId
     * @return
     */
    @GetMapping("/camp/{userId}/{currentPage}/{pageSize}")
    public Result findAllCamp(@PathVariable Integer userId, @PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<CourseDetailsCamp> camps = userService.findAllCamp(userId, currentPage, pageSize);
        Result result = new Result();
        if (camps != null) {
            PageInfo<CourseDetailsCamp> pageInfo = new PageInfo<>(camps);
            result.setCode(200);
            result.setMsg("训练营查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("训练营查询失败");
        }
        return result;
    }

    @GetMapping("/{userId}")
    public Result findUserBalance(@PathVariable Integer userId){
        Double balance = userService.findUserBalance(userId);

        Result result = new Result();
        if(balance != null){
            result.setCode(200);
            result.setMsg("查询余额成功");
            result.setData(balance);
        }else {
            result.setCode(500);
            result.setMsg("查询余额失败");
        }
        return result;
    }

    /**
     * 查询用户的所有课程订单
     * @param userId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/order/{userId}/{currentPage}/{pageSize}")
    public Result findAllCourseOrder(@PathVariable Integer userId, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CourseOrderDetails> orderDetails = userService.findAllCourseOrder(userId, currentPage, pageSize);
        System.out.println(orderDetails);

        Result result = new Result();
        if (orderDetails != null) {
            PageInfo<CourseOrderDetails> pageInfo = new PageInfo<>(orderDetails);
            result.setCode(200);
            result.setMsg("课程订单查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("课程订单查询失败");
        }
        return result;

    }

    /**
     * 查询用户的课程订单
     * 根据订单的状态查询
     * @param userId
     * @param status
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/order/{userId}/{status}/{currentPage}/{pageSize}")
    public Result findCourseOrder(@PathVariable Integer userId, @PathVariable Integer status, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CourseOrderDetails> orderDetails = userService.findCourseOrder(userId, status, currentPage, pageSize);
        System.out.println(orderDetails);

        Result result = new Result();
        if (orderDetails != null) {
            PageInfo<CourseOrderDetails> pageInfo = new PageInfo<>(orderDetails);
            result.setCode(200);
            result.setMsg("课程订单，根据状态查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("课程订单，根据状态查询失败");
        }
        return result;

    }


    @GetMapping("/campOrder/{userId}/{currentPage}/{pageSize}")
    public Result findAllCampOrder(@PathVariable Integer userId, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CourseOrderDetails> orderDetails = userService.findAllCampOrder(userId, currentPage, pageSize);
        System.out.println(orderDetails);

        Result result = new Result();
        if (orderDetails != null) {
            PageInfo<CourseOrderDetails> pageInfo = new PageInfo<>(orderDetails);
            result.setCode(200);
            result.setMsg("用户训练营查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("用户训练营查询失败");
        }
        return result;

    }

    @GetMapping("/campOrder/{userId}/{status}/{currentPage}/{pageSize}")
    public Result findCampOrder(@PathVariable Integer userId, @PathVariable Integer status, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<CourseOrderDetails> orderDetails = userService.findCampOrder(userId, status, currentPage, pageSize);
        System.out.println(orderDetails);

        Result result = new Result();
        if (orderDetails != null) {
            PageInfo<CourseOrderDetails> pageInfo = new PageInfo<>(orderDetails);
            result.setCode(200);
            result.setMsg("用户训练营，根据状态查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("用户训练营，根据状态查询失败");
        }
        return result;

    }


}
